package com.tech.arno.spicy.ui.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.tech.arno.spicy.R;
import com.tech.arno.spicy.mvp.presenterImpl.HomePresenterImpl;
import com.tech.arno.spicy.mvp.view.HomeView;
import com.tech.arno.spicy.ui.fragment.home.MessageFragment;
import com.tech.arno.spicy.ui.fragment.home.MineFragment;
import com.tech.arno.spicy.ui.fragment.home.NewsFragment;

/*
*   package:     com.tech.arno.spicy.ui.activity
*   fileName:    HomeActivity
*   author:    ArnoFrost
*   date:  2017/12/7 0007 9:42
*   desc:     HomeActivity新
*/

public class HomeActivity extends BaseActivity implements HomeView {

    private BottomNavigationBar bottomNavigationBar;

    private FragmentManager fm;
    private NewsFragment mNewsFragment;
    //    private Fragment mCommonFragmentOne;
    private MessageFragment mMessageFragment;
    private MineFragment mMineFragment;
    private Fragment mCurrent;

    private HomePresenterImpl presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_layout);
        initView();
        //添加Fragment
        mNewsFragment = new NewsFragment();
        fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.content_layout, mNewsFragment);
        fragmentTransaction.commit();
        presenter = new HomePresenterImpl(this);
    }

    private void initView() {
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        setNavigation();
    }

    @Override
    protected void onDestroy() {
        //在Activity销毁时一定要释放资源
        presenter.onDestroy();
        super.onDestroy();
    }

    @Override
    public void setNavigation() {
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.comui_tab_home, "主页"))
                .addItem(new BottomNavigationItem(R.drawable.comui_tab_message, "消息"))
                .addItem(new BottomNavigationItem(R.drawable.comui_tab_person, "个人中心"))
//                .addItem(new BottomNavigationItem(R.drawable.comui_tab_pond, "Movies & TV"))
                .initialise();

        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                FragmentTransaction fragmentTransaction = fm.beginTransaction();

                switch (position) {
                    case 0:
                        hideFragment(mMessageFragment, fragmentTransaction);
                        hideFragment(mMineFragment, fragmentTransaction);
                        if (mNewsFragment == null) {
                            mNewsFragment = new NewsFragment();
                            fragmentTransaction.add(R.id.content_layout, mNewsFragment);
                        } else {
                            mCurrent = mNewsFragment;
                            fragmentTransaction.show(mNewsFragment);
                        }

                        //showHomeFragment();
                        break;
                    case 1:
                        hideFragment(mNewsFragment, fragmentTransaction);
                        hideFragment(mMineFragment, fragmentTransaction);
                        if (mMessageFragment == null) {
                            mMessageFragment = new MessageFragment();
                            fragmentTransaction.add(R.id.content_layout, mMessageFragment);
                        } else {
                            mCurrent = mMessageFragment;
                            fragmentTransaction.show(mMessageFragment);
                        }
                        break;
                    case 2:
                        hideFragment(mMessageFragment, fragmentTransaction);
                        hideFragment(mNewsFragment, fragmentTransaction);
                        if (mMineFragment == null) {
                            mMineFragment = new MineFragment();
                            fragmentTransaction.add(R.id.content_layout, mMineFragment);
                        } else {
                            mCurrent = mMineFragment;
                            fragmentTransaction.show(mMineFragment);
                        }
                        break;
                }
                fragmentTransaction.commit();

            }

            @Override
            public void onTabUnselected(int position) {
            }

            @Override
            public void onTabReselected(int position) {
            }
        });
    }

    @Override
    public void hideFragment(Fragment fragment, FragmentTransaction fragmentTransaction) {
        if (fragment != null) {
            fragmentTransaction.hide(fragment);
        }
    }
}
